package com.xingxue.controller;

import com.xingxue.service.ClientService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;


/**
 * @author zgx at 2018/10/23 0023 下午 12:01
 */

@Controller
@RequestMapping("client")
public class ClientContreller {
    Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);

    @Autowired
    ClientService clientService;

    @RequestMapping(value = "/search")
    public ModelAndView search(){
        ModelAndView mv = new ModelAndView();
        List<HashMap<String,Object>> search = this.clientService.search();
        mv.addObject("search",search);
        mv.setViewName("accountManger");
        return mv;
    }

    @RequestMapping(value = "/add")
    public ModelAndView add(@RequestParam HashMap<String,Object> param){
        this.clientService.add(param);
        return new ModelAndView("redirect:search.do");
    }

    @RequestMapping(value = "/updateById")
    public ModelAndView updateById(@RequestParam HashMap<String,Object> param){
        this.clientService.updateByID(param);
        return new ModelAndView("redirect:search.do");
    }

    @RequestMapping(value = "/deleteById")
    public ModelAndView deleteById(@RequestParam HashMap<String,Object> param){
        this.clientService.deleteByID(param);
        return new ModelAndView("redirect:search.do");
    }

    @RequestMapping(value = "/deleteCheckedById")
    public ModelAndView deleteById(@RequestParam String num){
        List<String> param = Arrays.asList(num.split(","));
        this.clientService.deleteCheckedByID(param);
        return new ModelAndView("redirect:search.do");
    }

}
